//
//  receiveFirCirPicture.m
//  tcpoctest
//
//  Created by gump on 5/10/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "receiveFirCirPicture.h"

#import "FriendStatus.h"

#import "FirCirNewsDataSource.h"
#import "FirendCircleDataSource.h"
#import "FirCirLocalResourceDataSource.h"

#import "FirCirPictureIDdb.h"
#import "FirendCirLocalPathdb.h"

#import "sendFirCirPicture.h"

@implementation receiveFirCirPicture

-(void)processreceive
{
    NSString *newid = [logicsb ReadUnencodeString];
    NSString *picid = [logicsb ReadUnencodeString];
    
    FriendStatus *fs = [[DataControl shareControl].fcnewsDS getNewsByID:newid];
    
    if (fs != nil) {
        [fs.imageIDArray addObject:picid];
        [[DataBaseCtrol shareControl].fircirPIDdatabase insertPID:newid withPictureID:picid];
        
        UIImage *image = [[DataControl shareControl].fircirDS getPhotoArrObjectIndex:
                          [[DataControl shareControl].fircirDS SendPicCount]];
        NSData *imagedata = UIImageJPEGRepresentation(image, JPG_COMPRESS_RATE);
        NSString *savepath = [[DataControl shareControl].fcLocalRecDS creatRcLocalPath];
        
        FirCirLocalInfo *fclinfo = [[FirCirLocalInfo alloc] init];
        fclinfo.resourceID = picid;
        
        if ([imagedata writeToFile:savepath atomically:YES]) {
            fclinfo.localpath = savepath;
        }
        
        [[DataControl shareControl].fcLocalRecDS AddLocalRs:fclinfo];
        [[DataBaseCtrol shareControl].fircirlocaldatabase insertLocalPath:fclinfo];
    }
    
    if ([[DataControl shareControl].fircirDS SendPicCount] + 1 <
        [[DataControl shareControl].fircirDS getphotoArrCount]) {
        
        [[DataControl shareControl].fircirDS SetSendPicCount:
         [[DataControl shareControl].fircirDS SendPicCount] + 1];
        
        UIImage *image = [[DataControl shareControl].fircirDS
                          getPhotoArrObjectIndex:[[DataControl shareControl].fircirDS SendPicCount]];
        
        if (image != nil) {
            [[netManager sharedNetManager].sendFCPic SendFCPicture:image withNewsID:fs.statusId];
            
            [[DataControl shareControl].fircirDS SetHudString:
             [NSString stringWithFormat:@"发送图片(%d/%d)",
              [[DataControl shareControl].fircirDS SendPicCount]
              ,[[DataControl shareControl].fircirDS getphotoArrCount]]];
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_UPDATESENDHUD];
        }
        
    }
    else
    {
        [[DataControl shareControl].fircirDS setHudStr:@"发送成功"];
        
        [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_UPDATESENDHUD];
        [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_SENDNEWSUCCESS];
    }
    
    [self ShowLog];
}

@end
